import * as request from '../constant/fetch-request'
import HTTP_CODE from '../constant/http-code'

export const getAssignmentQuizMark = (assignmentId, quizId, studentId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/assignments/${assignmentId}/quizzes/${quizId}/students/${studentId}/assignmentQuizMarks`)
      if (res.status === HTTP_CODE.OK) {
        dispatch({
          type: 'GET_ASSIGNMENT_QUIZ_MARK',
          data: res.body
        })
      }
    })()
  }
}
export const editAssignmentQuizMark = (id, assignmentId, quizId, studentId, ranking, evaluate) => {
  const data = {
    id,
    ranking,
    evaluate
  }
  return dispatch => {
    (async () => {
      const res = await request.update('../api/assignments/assignmentQuizMarks', data)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(getAssignmentQuizMark(assignmentId, quizId, studentId))
      }
    })()
  }
}

export const addAssignmentQuizMark = (assignmentId, quizId, studentId, ranking, evaluate) => {
  const data = {
    assignmentId,
    quizId,
    studentId,
    ranking,
    evaluate
  }
  return dispatch => {
    (async () => {
      const res = await request.post('../api/assignments/assignmentQuizMarks', data)
      if (res.status === HTTP_CODE.CREATED) {
        dispatch(getAssignmentQuizMark(assignmentId, quizId, studentId))
      }
    })()
  }
}
